<?php

namespace app\models;

use Yii;
use yii\db\Query;
use app\models\BaseGoodTypes;
use app\models\Users;
use app\models\Order;
use app\models\OrderDetail;
use common\models\BaseImg;
/**
 * This is the model class for table "{{%cart}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $create_at
 * @property integer $status
 */
class Commodity extends \yii\db\ActiveRecord {

  public function SelNewsQg(){
      
      $goodsqg = (new Query())->select("a.*,c.img_url")->from("dh_base_goods_qg as a")->leftJoin("dh_base_goods as b","a.sp_id=b.id")->leftJoin("dh_base_img as c","b.img_id=c.id")->where(['a.status'=>1])->orderBy('a.fbsj DESC')->limit(20)->offset(0)->all();
      return $goodsqg;
  }
  
  public function SelBaseGoods($param) {
      $condition = $this->SelBaseGoodstype($param);
      $basegoods = (new Query())->select("a.*,b.img_url")->from("dh_base_goods as a")->leftJoin('dh_base_img as b','a.img_id=b.id')->where(['a.status'=>1])->andWhere('1=1'.$condition)->orderBy('a.salse DESC')->limit(2)->offset(0)->all();
      return $basegoods;
  }
  
  public function SelBaseGoods2($param) {
      $condition = $this->SelBaseGoodstype($param);
      $basegoods = (new Query())->select("a.*,b.img_url")->from("dh_base_goods as a")->leftJoin('dh_base_img as b','a.img_id=b.id')->where(['a.status'=>1])->andWhere('1=1'.$condition)->orderBy('a.salse DESC')->limit(4)->offset(2)->all();
      return $basegoods;
  }
  
  public function SelBaseGoodstype($type_id) {
     $category = ''; 
     $condition = '';
     $data =  BaseGoodTypes::upBasegoods($type_id);
     foreach ($data as $value) {
        $category .= $value['id'].',';
     }
     $category = substr($category, 0,-1);
     $condition .= ' and a.type_id in('.$category .')';
     return $condition;
  }
  
  public static function SelBaseBrands() {
      
     $basebrands = (new Query())->select("*")->from("dh_base_brand")->where(['is_deleted'=>0])->orderBy('id ASC')->limit(9)->offset(0)->all();
//     print_r($basebrands);die();
     return $basebrands;
  }
  
   public static function ListGoods1($id,$limit,$count){
            $str = BaseGoodTypes::getTypeChild($id);
             $sql = "select a.*,MIN(b.hsdj) as minhy,COUNT(b.sp_id) as sumhy,
                        MAX(c.cgjg) as maxqg ,COUNT(c.sp_id) as sumqg,d.img_url
                        from dh_base_goods as a
                        LEFT JOIN (select * from dh_base_goods_hy where status=1 and flag =1 ) as b on a.id = b.sp_id 
                        LEFT JOIN (select * from dh_base_goods_qg where status =1 and flag =1) as c on a.id = b.sp_id 
                        LEFT JOIN dh_base_img as d on a.img_id=d.id
                        where a.status = 1 and a.type_id in(".$str.") GROUP BY a.id ORDER BY a.created_at DESC limit ".$limit.",".$count;
             $goods = BaseGoodTypes::findBySql($sql)->asArray()->all();
             return $goods;
        }

      public static function getWebAd($page){
        $webad = (new Query())->select("a.*,b.img_url")->from("dh_web_ad as a")->leftJoin("dh_base_img as b","a.img_id=b.id")->where("a.status=0")->orderBy("a.sort DESC")->limit($page)->offset(0)-> all();
        return $webad;
    }
    
    public static function getBrand(){
        $brand = (new Query())->select("a.*,b.img_url")->from("dh_base_brand as a")->leftJoin("dh_base_img as b","a.img_id=b.id")->where("a.show=1")-> all();
//        print_r($brand);die();
        return $brand;
    }
    
     public static function getHot(){
        $hot = (new Query())->select("a.*,b.img_url,c.*")->from("dh_base_goods_hot as a")->leftJoin("dh_base_img as b","a.img_id=b.id")->leftJoin("dh_base_goods as c","c.id=a.goodsId")-> where("a.status=0")->orderBy("a.sort DESC")-> all();
//        print_r($hot);die();
        return $hot;
    }
    
    public static function getImgId($id){
        $imgurl = BaseImg::findOne($id);
       // var_dump($imgurl);die();
        return $imgurl->img_url;
    }
    
    public static function getOrderDeal(){
        $order = Order::find()->where('status=4')->orderBy('finish_time DESC')->limit(15)->offset(0)->asArray()->all();
        $OrderDeal = [];
        if (is_array($order)) {
            foreach ($order as $key => $value) {
                $OrderDetail = OrderDetail::find()->where('orderId='.$value['orderId'])->asArray()->one();
                $OrderDeal[$key]['name'] = $value['address_name'];
                $OrderDeal[$key]['goodsname'] = $OrderDetail['title'];
                $OrderDeal[$key]['quantity'] = $OrderDetail['quantity'];
            }
        }
        return $OrderDeal;
    }
}
